<?php 
global $_GPC, $_W;
$shopid=check_right(3);
if(!$shopid)message("请先选择需要管理的店铺",$this->createWebUrl('shop'),'error');
$shop=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_group')." WHERE weid = '{$_W['uniacid']}' and id='".$shopid."'");
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];
load()->model('mc');
/**/
if($operation=="display"){
	$list=pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_marketing') . " WHERE weid = '{$_W['uniacid']}' and groupid='".$shopid."' order by displayorder asc, id desc");
	$grouptemp=mc_groups();
	
	
} elseif ($operation == 'updateMove') {
	foreach($_GPC['displayorder'] as $index=>$row){
		pdo_update('j_hmoney_marketing',array("displayorder"=>$row),array("id"=>$index));
	}
	die(json_encode(array("success"=>true)));
	
} elseif ($operation == 'post') {
	$id=$_GPC['id'];
	if($id){
		$item=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_marketing') . " WHERE weid = '{$_W['uniacid']}' and id=:a ",array(':a'=>$id));
	}else{
		$item=array(
			"starttime"=>strtotime(date('Y-m-d')),
			"endtime"=>strtotime(date('Y-m-d')),
		);
	}
	$gamelist=pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_lotterygame') . " WHERE weid = '{$_W['uniacid']}' order by id desc");
	$grouptemp=mc_groups();
	
} elseif ($operation == 'postdata') {
	$id=intval($_GPC['id']);
	$data=array(
		"weid"=>$_W['uniacid'],
		"groupid"=>$shopid,
		"title"=>$_GPC['title'],
		"status"=>intval($_GPC['status']),
		"starttime"=>strtotime($_GPC['gametime']['start']." 00:00:00"),
		"endtime"=>strtotime($_GPC['gametime']['end']." 23:59:59"),
		"gameid"=>intval($_GPC['gameid']),
		"limitweek"=>implode(",",$_GPC['limitweek']),
		"limithour"=>implode(",",$_GPC['limithour']),
		"num"=>intval($_GPC['num']),
		"ftype"=>$_GPC['ftype'],
		"fee"=>$_GPC['fee'],
		"content"=>$_GPC['content'][$_GPC['ftype']],
		"memberlevel"=>implode(",",$_GPC['memberlevel']),
		"description"=>$_GPC['description'],
	);
	if($id){
		unset($data['weid']);unset($data['groupid']);
		pdo_update("j_hmoney_marketing",$data,array("id"=>$id));
	}else{
		pdo_insert("j_hmoney_marketing",$data);
	}
	die(json_encode(array("success"=>true)));

} elseif ($operation == 'delete') {
    $id=intval($_GPC['id']);
    $item=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_marketing') . " WHERE weid = '".$_W['uniacid']."' and id=:a ",array(':a'=>$id));
    if(!$item)message("活动不存在");
    pdo_delete("j_hmoney_marketing",array("id"=>$item["id"]));
    message("删除成功", $this->createWebUrl('market'), 'success');


} elseif ($operation == 'goodmarket') {
	$list=pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_goodmarket') . " WHERE weid = '{$_W['uniacid']}' and groupid='".$shopid."' order by status desc,id desc");
	
} elseif ($operation == 'goodmarketpost') {
	$id=$_GPC['id'];
	if($id){
		$item=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_goodmarket') . " WHERE weid = '{$_W['uniacid']}' and id=:a ",array(':a'=>$id));
	}else{
		$item=array(
			"starttime"=>strtotime(date('Y-m-d')),
			"endtime"=>strtotime(date('Y-m-d')),
		);
	}
	$goodcatelist=pdo_fetchall("SELECT id,title FROM ".tablename('j_hmoney_goodcategory') . " WHERE weid = '{$_W['uniacid']}' and groupid='".$shopid."' order by displayorder asc, id desc");
	
} elseif ($operation == 'selectpcate') {
	$cid=intval($_GPC['cid']);
	$selectid=($_GPC['selectid']) ? $_GPC['selectid'] : 0;
	$where=$cid ? " and pcate='".$cid."' and id not in (".$selectid.") ":" and id in (".$selectid.")";
	$goodcatelist=pdo_fetchall("SELECT id,title,thumb,marketprice FROM ".tablename('j_hmoney_goods') . " WHERE weid ='{$_W['uniacid']}' and groupid='".$shopid."' $where order by displayorder asc, id desc");
	die(json_encode(array("success"=>true,"list"=>$goodcatelist)));
	
	
} elseif ($operation == 'postgoodata') {
	$id=intval($_GPC['id']);
	$data=array(
		"weid"=>$_W['uniacid'],
		"groupid"=>$shopid,
		"title"=>$_GPC['title'],
		"status"=>intval($_GPC['status']),
		"starttime"=>strtotime($_GPC['gametime']['start']." 00:00:00"),
		"endtime"=>strtotime($_GPC['gametime']['end']." 23:59:59"),
		"limitweek"=>implode(",",$_GPC['limitweek']),
		"limithour"=>implode(",",$_GPC['limithour']),
		"discount"=>trim($_GPC['discount']),
		"contentid"=>trim($_GPC['contentid']),
		"description"=>$_GPC['description'],
		"createtime"=>TIMESTAMP,
	);
	if($id){
		unset($data['weid']);unset($data['groupid']);unset($data['createtime']);
		pdo_update("j_hmoney_goodmarket",$data,array("id"=>$id));
	}else{
		$result=pdo_insert("j_hmoney_goodmarket",$data);
	}
	die(json_encode(array("success"=>true)));


} elseif ($operation == 'deletegood') {
    $id=intval($_GPC['id']);
    $item=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_goodmarket') . " WHERE weid = '".$_W['uniacid']."' and id=:a ",array(':a'=>$id));
    if(!$item)message("活动不存在");
    pdo_delete("j_hmoney_goodmarket",array("id"=>$item["id"]));
    message("删除成功", $this->createWebUrl('market',array("op"=>"goodmarket")), 'success');
}
include $this->template('web/'.$cfg['floder'].'/market');